package com.foreveross.push.client;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

/**
 * [通知服务]<BR>
 * [功能详细描述]
 * 
 * @author 冯伟立
 * @version [CubeAndroid, 2013-7-16]
 */
public class NotificationService extends Service {
	private static final String TAG="NotificationService";
	public static final String ACTION_CONNECT_XMPP = "action_connect_xmpp";
	public static final String ACTION = "server_action";
	public static final String KEY_USERNAME = "userName";
	public static final String KEY_PASSWORD = "password";



	private XmppManager pushManager;
	private BroadcastReceiver connectivityReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			ConnectivityManager connectivityManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);

			NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
			XmppManager manager = pushManager;
			Log.v(TAG, "intent>>"+intent);
			if (pushManager != null) {
				if (networkInfo != null && networkInfo.isConnected()) {
					if (!isConnected(manager)) {
						Log.i("ConncetivityReceiver", "Network connected,begin reconnect to xmpp");
						reconnect(manager);
					} else {
						Log.i("ConncetivityReceiver", "Network unavailable,begin to close xmpp");
					}
				} else {
					disconnect(manager);
				}
			}
		}
	};

	@Override
	public void onCreate() {
		Log.d("NotificationService", "NotificationService  onCreate()...");
		if (pushManager == null) {
			pushManager = new XmppManager(this);
			pushManager.prepairConnect();
			IntentFilter filter = new IntentFilter();
			filter.addAction(android.net.ConnectivityManager.CONNECTIVITY_ACTION);
			registerReceiver(connectivityReceiver, filter);
		}

	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		if (intent != null) {
			String action = intent.getStringExtra(ACTION);
			if (action != null && action.equals(ACTION_CONNECT_XMPP)) {
				String password = intent.getStringExtra(KEY_PASSWORD);
				String userName = intent.getStringExtra(KEY_USERNAME);
				connect(userName, password);
			}

		}
		return START_STICKY;

	}

	@Override
	public void onDestroy() {
		Log.d(TAG, "notificationService onDestroy()...");
		stop();
	}

	@Override
	public IBinder onBind(Intent intent) {
		Log.d(TAG, "notificationService onBind()...return null");
		return null;
	}

	public void connect(final String username, final String password) {
		Log.d(TAG, "start connect to xmpp for user {}" + username);
		if (pushManager != null && !pushManager.isAuthenticated()) {
			pushManager.submitConnectReq(username, password);
		}

	}

	public void disconnect(final XmppManager manager) {
		Log.d(TAG, "start disconnect to xmpp");
		if (manager != null) {
			if (manager.isConnected()) {
				manager.disconnect();
			}
		}
	}

	public boolean isConnected(XmppManager manager) {
		return manager.isConnected();
	}


	public void reconnect(XmppManager manager) {
		Log.d(TAG, "notification reconnect()...");
		manager.reconnect();
	}

	private void stop() {
		Log.d(TAG, "notification stop()...");
		if(pushManager!=null){
			pushManager.disconnectNow();
		}
		unregisterReceiver(connectivityReceiver);
	}

	public boolean isOnline(XmppManager manager) {
		return manager.isOnline();
	}

}
